package com.zyfdroid.bigdialerkit;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.github.heqiao2010.lunar.LunarCalendar;
import com.zyfdroid.bigdialerkit.utils.BaseActivity;

import java.util.Calendar;

public class CalendarActivity extends BaseActivity {

    private TextView lblLunarDate; // 显示农历日期，但目前请固定赋值为XXX
    private TextView lblDayOfWeek; // 显示星期一 星期二灯内容
    private TextView lblToday; // 显示阿拉伯数字的dayOfMonth
    private TextView lblWhichDay; // 前天，昨天，今天，明天，后天，大后天
    private TextView lblSolarYearMonth; // 2025年 九月
    private void initUI(){
        lblLunarDate = (TextView)findViewById(R.id.lblLunarDate);
        lblDayOfWeek = (TextView)findViewById(R.id.lblDayOfWeek);
        lblToday = (TextView)findViewById(R.id.lblToday);
        lblWhichDay = (TextView)findViewById(R.id.lblWhichDay);
        lblSolarYearMonth = (TextView)findViewById(R.id.lblSolarYearMonth);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        initUI();
        setData(currentDayDiff);
        findViewById(R.id.btnPrevDay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData(--currentDayDiff);
            }
        });
        findViewById(R.id.btnNextDay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData(++currentDayDiff);
            }
        });
    }

    private int currentDayDiff = 0;
    public void setData(int dayDiff){
        // 1. 使用Calendar类进行日期计算
        Calendar calendar = Calendar.getInstance();

        // 为了避免时间点影响日期计算，将时分秒毫秒清零
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 根据dayDiff增加或减少天数
        calendar.add(Calendar.DAY_OF_MONTH, dayDiff);

        // 2. 从Calendar对象中提取所需信息
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH); // 0-11
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 1-7 (周日-周六)

        // 3. 准备中文格式的数据
        final String[] chineseMonths = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
        final String[] chineseDaysOfWeek = {"", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        // 4. 根据控件注释，填充数据

        // 农历日期，固定为XXX
        lblLunarDate.setText(new LunarCalendar(calendar).getLunar(true));

        // 星期几
        lblDayOfWeek.setText(chineseDaysOfWeek[dayOfWeek]);

        // 阿拉伯数字的dayOfMonth
        lblToday.setText(String.valueOf(day));

        // 前天，昨天，今天，明天，后天，大后天
        String whichDayStr;
        switch (dayDiff) {
            case -2: whichDayStr = "前天"; break;
            case -1: whichDayStr = "昨天"; break;
            case 0:  whichDayStr = "今天"; break;
            case 1:  whichDayStr = "明天"; break;
            case 2:  whichDayStr = "后天"; break;
            case 3:  whichDayStr = "大后天"; break;
            default:
                // 如果超出这个范围，可以显示具体日期，例如 "十月三日"
                whichDayStr = "  ";
                break;
        }
        lblWhichDay.setText(whichDayStr);

        // 2025年 九月
        lblSolarYearMonth.setText(year + "年 " + chineseMonths[month]);
    }
    public void contactGoBack(View view) {
        finish();
    }

}
